<template>
  <page-form :x-uri="uri" :bind-options="options" :custom-components="custom" :bind-rules="rules" :x-tabs="tabs" :bind-data="datas" />
</template>

<script>
import PageForm from '@public/template/page_form.vue';
import ChooseImage from '@/pages/material/choose/image.vue';
import VipPrice from '../video/components/vip_price.vue';
import SearchTeacher from '@/pages/user/teacher/choose/index.vue';
import SetTermTime from '../video/components/term_time';
export default {
  components: {
    PageForm
  },
  data() {
    return {
      uri: 'course/admin/live/create',
      tabs: [
        {
          title: '大班课',
          uri: '/course/live/list',
          meta: {
            permission: 'course.live.large'
          }
        },
        {
          title: '创建大班课',
          uri: '/course/live/create',
          meta: {
            permission: 'course.live.large.create'
          }
        }
      ],
      options: {
        listing_time: {
          disabledDate: function(time) {
            return time.getTime() + 8.64e7 < Date.now();
          },
          shortcuts: [
            {
              text: '立即上架',
              onClick: function(picker) {
                picker.$emit('pick', new Date());
              }
            },
            {
              text: '明日凌晨',
              onClick: function(picker) {
                const current = new Date();
                const nowDate = (current.getHours() * 60 * 60 + current.getMinutes() * 60 + current.getSeconds()) * 1000;
                const nextDate = new Date(current.getTime() + 86400000 - nowDate);
                console.log(nextDate);
                picker.$emit('pick', nextDate);
              }
            }
          ]
        }
      },
      datas: {
        // 如果为数组,表示多文件上传,所有上传的ID,都会存到数组里面
        // 如果是整形,表示单个文件上传,后上传的文件ID,会覆盖之前的ID
        cover: 0,
        status: 1,
        exam_condition_rate: 100
      },
      custom: {
        cover: ChooseImage,
        vip_price: VipPrice,
        teacher_id: SearchTeacher,
        term_time: SetTermTime
      },
      rules: {
        cover: [
          {
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error('请上传课程封面'));
              }
              callback();
            },
            trigger: ['blur', 'change'],
            required: true
          }
        ],
        exam_condition_rate: [
          {
            validator: (rule, value, callback) => {
              const reg = /^([1-9]?\d|100)$/;
              if (!reg.test(value)) {
                return callback(new Error('必须为0-100的整数'));
              }
              callback();
            },
            trigger: ['blur', 'change']
          }
        ]
      }
    };
  }
};
</script>
